<template>
  <div>
    <!--头部搜索框-->
    <van-search
      v-model="searchGoods"
      show-action
      label="宝贝"
      background="#eee"
      placeholder="请输入搜索关键词"
      @click="onSearch"
    >
      >
      <template #action>
        <div @click="getHome">
          <i class="iconfont icon-shouye"></i>
        </div>
      </template>
    </van-search>
    <van-dropdown-menu>
      <van-dropdown-item v-model="value1" :options="option1" :change="gotop" />
      <van-dropdown-item v-model="value2" :options="option2" />
      <van-dropdown-item v-model="value3" :options="option3" />
    </van-dropdown-menu>
    <div class="recommend">
      <div class="recommend-container">
        <ul>
          <li v-for="(item, index) in goodslist" :key="index">
            <router-link :to="{ name: 'listdetailed', query: { id: item.goodsId } }">
              <img :src="JSON.parse(item.bannerImg)[0]" alt />
              <p>{{ item.title }}</p>
              <p>
                <span>￥{{ item.price }}</span>
                <em>{{ item.goodComments }}已购买</em>
              </p>
            </router-link>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import indexApi from "@/api/indexApi";
export default {
  data() {
    return {
      searchGoods: null,
      value1: 0,
      value2: "a",
      value3: "x",
      goodslist: null,
      option1: [
        { text: "综合排序", value: 0 },
        { text: "价格从高到底", value: 1 },
        { text: "价格从底到高", value: 2 },
        { text: "信用排序", value: 3 }
      ],
      option2: [{ text: "销量优先", value: "a" }],
      option3: [{ text: "筛选", value: "x" }]
    };
  },

  components: {},

  methods: {
    //
    gotop(value) {
      console.log(value);
    },
    onSearch() {
      this.$router.push({ name: "Search", query: { url: "List" } });
    },
    getHome() {
      this.$router.push({ path: "/home" });
    },
    async getlist() {
      try {
        let p = await indexApi.getallgoods();
        this.goodslist = p.data.data;
      } catch (err) {
        console.log(err);
      }
    }
  },
  created() {
    this.getlist();
  }
};
</script>

<style scoped lang="scss">
.recommend {
  padding: 8px;
  .recommend-container {
    width: 100%;
    ul {
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;

      li {
        box-sizing: border-box;
        background-color: #fff;
        width: 49%;
        margin-top: 8px;
        border-radius: 10px;
        overflow: hidden;
        img {
          width: 100%;
          height: 170px;
          display: block;
        }
        p {
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          font-size: 14px;
          margin: 10px 0;
          span {
            color: #ff6b51;
          }
          em {
            margin-left: 5px;
            color: rgb(168, 168, 168);
          }
        }
      }
    }
  }
}
</style>
